
//搜索框函数
$('.ui-searchbar').tap(function(){
    $('.ui-searchbar-wrap').addClass('focus');
    $('.ui-searchbar-input input').focus();
});
$('.ui-searchbar-cancel').tap(function(){
    $('.ui-searchbar-wrap').removeClass('focus');
});

//选项卡函数
(function (){
    var tab = new fz.Scroll('.ui-tab', {
        role: 'tab',
        autoplay: false
    });
})();

//页面加载和点击执行最近常用
    function recently(){
        var user=localStorage.nameQQ;
        $.ajax({
            type:"post",
            url:"/recently",
            data:{user:user},
            success:function(date){
                if(date=="error"){
                    return false;
                }
                var html="";
                for(var i=0;i<date.length;i++){
                    html+='<li class="ui-border-b">'+date[i].r_name+'</li>';
                }
                $('.recentlyBox').html(html);
            }
        });
        $('.clickRecently').on('tap',function(){
            $.ajax({
                type:"post",
                url:"/recently",
                data:"",
                success:function(date){
                    if(date=="error"){
                        return false;
                    }
                    var html="";
                    for(var i=0;i<date.length;i++){
                        html+='<li class="ui-border-b">'+date[i].r_name+'</li>';
                    }
                    $('.recentlyBox').html(html);
                }
            });
        })
    }
recently();

//点击热门车站加载热门车站
    function hot(){
        $('.clickHot').on('tap',function(){
            $.ajax({
                type:"post",
                url:"/hot",
                data:"",
                success:function(date){
                    if(date=="error"){
                        return false;
                    }
                    var html="";
                    for(var i=0;i<date.length;i++){
                        html+='<li class="ui-border-b">'+date[i].h_name+'</li>';
                    }
                    $('.hotBox').html(html);
                }
            });
        });
    }
hot();

//点击车站列表加载所有车站
    function all(){
        $('.clickAll').on('tap',function(){
            $.ajax({
                type:"post",
                url:"/all",
                data:"",
                success:function(date){
                    if(date=="error"){
                        return false;
                    }
                    var html='<li class="ui-border-b firatChild">(ALL)</li>';
                    for(var i=0;i<date.length;i++){
                        html+='<li class="ui-border-b">'+date[i].c_name+'</li>';
                    }
                    $('.allBox').html(html);
                }
            });
        });
    }
all();

//车站列表右边“锚点“单机事件
    function miaoDian(){
        $('.miaoDian li').on('tap',function(){
            var val=$(this).text();
            var obj={
                val:val
            };
            $.ajax({
                type:"post",
                url:"/miaoDian",
                data:obj,
                success:function(date){
                    if(date=="error"){
                        var html=' <li class="ui-border-b firatChild">'+val+'</li>';
                    }else{
                        var html=' <li class="ui-border-b firatChild">'+val+'</li>';
                        for(var i=0;i<date.length;i++){
                            html+='<li class="ui-border-b">'+date[i].c_name+'</li>';
                        }
                    }

                    $('.allBox').html(html);
                }
            })
        });

    }
miaoDian();

//选择最近车站中的站点作为出发或者结束站点
    function renChoose(){
        $(document).on('tap',".recentlyBox>li",function(){
            var myChoose=$(this).text();
            localStorage.setItem("myChoose",myChoose);
            location.href="ticketBooking.html";
        })
    }
renChoose();

//选择热门车站中的站点作为出发或者结束站点
    function hotChoose(){
        $(document).on('tap',".hotBox>li",function(){
            var myChoose=$(this).text();
            localStorage.setItem("myChoose",myChoose);
            location.href="ticketBooking.html";
        })
    }
hotChoose();

//选择全部车站中的站点为出发或者结束站点
    function allChoose(){
        $(document).on('tap',".allBox>li:not(.firatChild)",function(){
            var myChoose=$(this).text();
            localStorage.setItem("myChoose",myChoose);
            location.href="ticketBooking.html";
        })
    }
allChoose();



//页面加载保存选项卡状态
function storageCard(){
   localStorage.setItem("cardChoose","1");
    $('.cardBox>li').on('tap',function(){
        var count=$(this).attr("hehe");
        localStorage.setItem("cardChoose",count);
    });

}
storageCard();


//        搜索框事件
var myModlue=angular.module("myapp",[]);
myModlue.controller("control",function($scope,$http){
    $scope.$watch("searchInput",function(){
        var val=($scope.searchInput)+"%";
        var  obj={
            val:val
        };
        if(val=="undefined%"){
            return false;
        }
        if(localStorage.cardChoose=="1"){
            $http({
                method:"post",
                url:"/recentlySearch",
                data:obj
            }).then(
                function(reqData){
                    console.log(reqData);
                    if(reqData.data=="error"){
                        return false;
                    }
                    var html="";
                    for(var i=0;i<(reqData.data).length;i++){
                        html+='<li class="ui-border-b">'+reqData.data[i].r_name+'</li>';
                    }
                    $('.recentlyBox').html(html);
                },function(errorData){
                    console.log("传输错误");
                }
            );

        }else if(localStorage.cardChoose=="2"){
            $http({
                method:"post",
                url:"/hotSearch",
                data:obj
            }).then(
                function(reqData){
                    console.log(reqData);
                    if(reqData.data=="error"){
                        return false;
                    }
                    var html="";
                    for(var i=0;i<(reqData.data).length;i++){
                        html+='<li class="ui-border-b">'+reqData.data[i].h_name+'</li>';
                    }
                    $('.hotBox').html(html);
                },function(errorData){
                    console.log("传输错误");
                }
            );
        }else{
            $http({
                method:"post",
                url:"/allSearch",
                data:obj
            }).then(
                function(reqData){
                    console.log(reqData);
                    if(reqData.data=="error"){
                        return false;
                    }else{
                        var html="",count=0;
                        for(var i=0;i<(reqData.data).length;i++){
                            html+='<li class="ui-border-b">'+reqData.data[i].c_name+'</li>';
                            localStorage.setItem("searchInitials",reqData.data[i].c_Initials);
                            if(val=='%'){
                                localStorage.setItem("searchInitials","ALL");
                            }

                        }
                    }
                    var html2=' <li class="ui-border-b firatChild">'+localStorage.searchInitials+'</li>';
                    html2+=html;
                    $('.allBox').html(html2);
                },function(errorData){
                    console.log("传输错误");
                }
            );
        }
    })

});